﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace SerializationLab
{
    class Program
    {
        static void Main(string[] args)
        {
            //先运行这个
            Serialize();
            //再运行这个
            //Deserialize();
        }

        private static void Deserialize()
        {
            BinaryFormatter f = new BinaryFormatter();
            using (FileStream fs = new FileStream("d:\\entity.bin", FileMode.Open))
            {
                Entity e = f.Deserialize(fs) as Entity;
            }
        }

        static void Serialize()
        {
            Entity e = new Entity { Id = 1, Name = "j" };
            BinaryFormatter f = new BinaryFormatter();
            using (FileStream fs = new FileStream("d:\\entity.bin", FileMode.Create))
            {
                f.Serialize(fs, e);
            }
        }
    }

    [Serializable]
    public class Entity
    {
        public int Id { get; set; }
        public string Name
        { get; set; }

        //序列化的时候先注释掉，反序列化的时候取消注释
        //[OptionalField]
        //public int age;
    }
}
